package com.zzxz.practicaltrainingsys.controller;

import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Dailynews;
import com.zzxz.practicaltrainingsys.service.DailynewsService;
import com.zzxz.practicaltrainingsys.util.we5FileUtil;
import com.zzxz.practicaltrainingsys.vo.EditResult;
import com.zzxz.practicaltrainingsys.vo.editData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dailynews")
@CrossOrigin
public class DailynewsController {
    @Autowired
    private DailynewsService dailynewsService;

    //获取指定项目的所有学生日报信息
    //http://localhost:8080/dailynews/project?projectId=1
    @GetMapping("/project")
    public ResponseEntity<?> getDailyNewsByProject(
            @RequestParam Integer projectId) {
        try {
            List<Map<String, Object>> result = dailynewsService.getDailyNewsByProjectId(projectId);
            return ResponseEntity.ok(result);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("获取数据失败: " + e.getMessage());
        }
    }

    //http://localhost:8080/dailynews/uploadEdit
    @ApiOperation("WangEdict5 图片上传操作")
    @PostMapping("/uploadEdit")
    public EditResult WangEditUpload(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String filePath = we5FileUtil.getUpLoadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis() + typeName;

        try {
            we5FileUtil.uploadFile(file.getBytes(), filePath, newFileName);
        } catch (Exception e) {
            throw new RuntimeException("Failed to uploadEdit", e);
        }
        EditResult editResult = new EditResult();
        editData data = new editData();

        data.setUrl("http://localhost:8080/images/upload/we5/"+newFileName);
        editResult.setData(data);
        editResult.setErrno(0); //0成功，1失败
        return editResult;
    }

    //用来联合学生表的学号以及项目表的项目编号来查该项目该学生的日报
    //http://localhost:8080/dailynews/list
    @GetMapping("list")
    public List<Dailynews> getDailynewsByStudentIdAndProjectId(
            @RequestParam Integer studentId,
            @RequestParam Integer projectId) {
        return dailynewsService.getDailynewsByStudentIdAndProjectId(studentId,projectId);
    }

    //日报详情
    //http://localhost:8080/dailynews/detail?dailynewsId=3
    @GetMapping("/detail")
    public Dailynews getDailynewsById(@RequestParam Integer dailynewsId) {
        return dailynewsService.getDailynewsById(dailynewsId);
    }

    //新增日报
    //http://localhost:8080/dailynews/insert
    @PostMapping("/insert")
    public int insertDailynews(@RequestBody Dailynews dailynews) {
        int result = dailynewsService.insertDailynews(dailynews);
        return result;
    }

    //更新日报
    //http://localhost:8080/dailynews/update
    @PutMapping("/update")
    public int updateDailynews(@RequestBody Dailynews dailynews) {
        return dailynewsService.updateDailynews(dailynews);
    }

    //用来查询所有日报信息
    //http://localhost:8080/dailynews/all
    @GetMapping("/all")
    public List<Map<String, Object>> getAllDailyReports() {
        return dailynewsService.getAllDailyReports();
    }

    @ApiOperation("查找日报信息（分页查询）")
    @GetMapping("/pages")
    public PageInfo<Map<String, Object>> getDailyNewsByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return dailynewsService.getDailyNewsByPage(pageNum, pageSize);
    }

    @GetMapping("/allall")
    public List<Map<String, Object>> getAllDailyNews() {
        return dailynewsService.getAllDailyNews();
    }
}

